import { createDiscreteApi } from 'naive-ui'
import {changeFriend, getFriend, listChatUserFriend} from "@/api/weblog/chatUserFriend";
import {ref} from 'vue'
import {ElMessage} from "element-plus";
import * as $common from "@/utils/common";


const { dialog } = createDiscreteApi(["dialog"])
export  const friendRequests = ref([]); // 好友请求数组
export  const friends = ref({}); // 好友列表对象
export  const currentFriendId = ref(null); // 当前朋友ID
import useUserStore from "@/store/modules/user"
const store = useUserStore()
export  async function getImFriend() {
  let axiosResponse = await listChatUserFriend({userId:store.id,friendStatus:1});
  axiosResponse.rows.forEach(friend => {
          friends.value[friend.friendId] = friend
        })
  }

export  function removeFriend(currentFriendId) {
    dialog.error({
      title: '警告',
      content: '你确定删除' + friends.value[currentFriendId].remark + '?',
      positiveText: '确定',
      onPositiveClick: async () => {
        let res = await changeFriend({
          friendId: currentFriendId,
          friendStatus: -1
        });
        delete friends.value[currentFriendId]
        currentFriendId.value = null
        ElMessage({
          message: '删除成功！',
          type: 'success'
        })
      }
    })
  }

export async function getFriendRequests() {
  let res = await getFriend({ friendStatus: 0 });
  if (!$common.isEmpty(res.data)) {
    friendRequests.value = res.data
    ElMessage({
      message: '您有好友申请待处理！',
      showClose: true,
      type: 'success',
      duration: 0
    })
  }
  }

export async function changeFriendStatus(friendId, status, index) {

  let res = await changeFriend({friendId: friendId, friendStatus: status});
      friendRequests.value.splice(index, 1)
      ElMessage({
        message: '修改成功！',
        type: 'success'
      })
}

